﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace MainService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UserService : IUserService
    {

        public UserDetails ValidateUser(string username, string password)
        {
            UserDetails _user = new UserDetails();
            try
            {
                _user.UserName = username;
                _user.Password = password;
                _user = _user.ValidateUser();
            }
            catch (Exception ex)
            {
                _user.LoginMessage = ex.Message;
            }

            return _user;
        }

        public string AddUser(string emailId, string appUserName, string password, string company)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.EmailId = emailId;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = company;
                msgs=_usr.AddUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                _usr.LoginMessage = ex.Message;
            }
            return msgs;
        }
        
        public string AddUser(int userId,string emailId, string appUserName, string password, string company, int deviceLimit, int clientId, List<string> lstServices,string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.EmailId = emailId;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = company;
                _usr.ClientId = clientId;
                _usr.DeviceLimit = deviceLimit;
                _usr.ServiceList = lstServices;
                _usr.HashCode = hashCode;
                msgs = _usr.AddUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
               msgs = ex.Message;
            }
            return msgs;
        }


        public string UpdateUser(int userId, string appUserName, string password, int deviceLimit, List<string> lstServices, int clientId,string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.EmailId = "";
                _usr.HashCode = hashCode;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = "";
                _usr.ClientId = clientId;
                _usr.DeviceLimit = deviceLimit;
                _usr.ServiceList = lstServices;
                msgs = _usr.UpdateUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        public string DeleteUser(int userId, int clinetId,string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.HashCode = hashCode;
                _usr.ClientId = clinetId;
                msgs = _usr.DeleteUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }


        public List<UserDetails> ListUsers(int clientId, string HashCode)
        {
            return UserDetails.GetUserList(clientId, HashCode);
        }


        public UserDetails GetAppUser(int userId, int clientId, string hashCode)
        {
            return UserDetails.ListAppUser(userId, clientId, hashCode);
        }
    }
}
